//
//  xianhuoVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/1/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "xianhuoVC.h"
#import "Masonry.h"
#import "Controller.h"
#import "SVPullToRefresh.h"
#import "xianhuoList.h"
#import "xianhuoModel.h"
#import "CZtableview.h"
#import "labelview.h"
#import "Tool.h"
#import "TongYong.h"
#import "xianhuoNoimageTableViewCell.h"
#import "xianhuoDatailView.h"
#import "gouwuCarVC.h"
#import "UMMobClick/MobClick.h"
#import "ShaiXuanTiaoJianResultModel.h"
#import "CustomShaiXuanView.h"
#import "UILabel+StringFrame.h"
#import "MJExtension.h"
#import "YongHuQuanXuanModel.h"
#import "CreateCompanyVC.h"
#import "FindCompanyViewController.h"
#import "CompanyInviteCodeViewController.h"
#import "NSString+formatWithFloat.h"
@interface xianhuoVC () <UITableViewDataSource, UITableViewDelegate, labelviewdelegate,CZtableviewdelegate> {
    //蒙版
    UIView *bantouming;
    
    //顶部菜单栏
    UIView *docview;
    //定义三个按钮
    UIButton *CZstatus;//材质按钮
    UIButton *JGstatus;//价格按钮
    UIButton *SXbut;//筛选按钮
    
    //材质的表格试图
    UIView *shadeView;//遮罩试图
//    UITableView *_CZtableview;//材质的表格试图
    NSArray *CZarray;//定义材质的数组
    NSInteger indexRow;//选中的哪一行
    
    
     NSMutableArray *arrayList;//报价数组；
  
     CZtableview *cztableview;
     labelview *la;//无数据显示的页面
    
    xianhuoList *listdata;//现货列表数据
    
    NSString *namestr;//按钮名字
    NSArray *NameArray;
    NSArray *VuleArray;
    NSArray *HeadArray;
}
@property(nonatomic,strong) UITableView *tableview;  //定义大表格
@property (nonatomic, assign)NSInteger  page;//页数
@property (nonatomic,assign) int sort;  //排序方式（0：默认排序（发布时间降序），1：按销售价由低到高，2：按销售价由高到低）
@property (nonatomic,copy)NSString *CategoryName; //分类名 板、卷、管、型
@property (nonatomic,copy)NSString *MaterialMappingID;//材质映射ID
@property (nonatomic,copy)NSString *SurfaceMappingID;//表面映射ID
@property (nonatomic,copy)NSString *FactoryMappingID;//生产商映射ID
@property (nonatomic,copy)NSString *WeightRangeID;//重量范围ID
@property (nonatomic,copy)NSString *ThickCode;//厚度ID
@property (nonatomic,copy)NSString *IsOnlyDiscount;//是否特惠 传入 "true" 或 "false"
@property (nonatomic,copy)NSString *Supplier_ID;//供应商ID
@property (nonatomic,copy)NSString *WidthCode;//宽度ID
@property (nonatomic, strong) NSString *Varieties;  // 品类
@property (nonatomic, strong) NSMutableString *CityNameList; // 地区名称，多个用逗号隔开
@property (nonatomic, strong) CustomShaiXuanView *customShaiXuanView;// 自定义筛选View
@property (nonatomic, strong) ShaiXuanTiaoJianResultModel *shaiXuanTiaoJianResultModel; // 筛选model
@property (nonatomic, strong) NSArray *huaNanContainCityArray;                          // 华南数组
@property (nonatomic, strong) NSArray *selectedCityArray;                               // 选中的城市数组
@property (nonatomic, strong) NSMutableString *selectedCityString;                      // 选中的城市字符串

@end

@implementation xianhuoVC
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"xianhuoVC"];
    
    //以后进来的时候  让数据刷新
    if (![TongYong isBlankObjectAndStr:self.hometoxh]){
        //证明是从首页跳转过来的
        if (![TongYong isBlankObjectAndStr:self.homeViewDic]) {
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"factoryCode"]]) {
                //生产商编码
                self.FactoryMappingID=self.homeViewDic[@"factoryCode"];
                
            }else{
                
                self.FactoryMappingID=@"";//生产商
                
            }
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"materialCode"]]) {
                //材质编码
                self.MaterialMappingID=self.homeViewDic[@"materialCode"];
                
            }else{
                
                self.MaterialMappingID=@"";//材质
                
            }
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"supplierId"]]) {
                //供应商编码
                self.Supplier_ID=self.homeViewDic[@"supplierId"];
                
            }else{
                
                self.Supplier_ID=@"";
                
            }
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"onlyDiscount"]]) {
                //是否特惠
                NSInteger isonly=[self.homeViewDic[@"onlyDiscount"] integerValue];
                if (isonly==0) {
                    //全部
                    self.IsOnlyDiscount=@"";
                    
                }else if(isonly==1){
                    
                    self.IsOnlyDiscount=@"true";
                    
                }
            }else
            {
                self.IsOnlyDiscount=@"";//是否特惠
                
            }
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"category"]]) {
                //产品类型
                self.CategoryName=self.homeViewDic[@"category"];
                
            }else{
            
                self.CategoryName=@"卷";
            }
            
            if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"supplierId"]]) {
                //产品类型
                self.Supplier_ID=self.homeViewDic[@"supplierId"];
                
            }
            self.WeightRangeID=@"";//重量
            self.ThickCode=@"";//厚度
            self.SurfaceMappingID=@"";//表面
            self.WidthCode=@"";//宽度
            
            if ([self.CategoryName isEqualToString:@"卷"]) {
                namestr=@"卷材";
            }else if([self.CategoryName isEqualToString:@"板"]){
                
                namestr=@"板材";
            }else if([self.CategoryName isEqualToString:@"管"]){
                
                namestr=@"管材";
            }else if([self.CategoryName isEqualToString:@"型"]){
                
                namestr=@"型材";
            }else if([self.CategoryName isEqualToString:@"带"]){
                
                namestr=@"带材";
            }


            //改变第一个选项的按钮 名称
            [CZstatus setTitle:namestr forState:UIControlStateNormal];
            
            //改变其内部的选择表格的构造
            
            if (cztableview) {
                
                [cztableview removeFromSuperview];
            }
            [self upCZtibView];
            
            

            //刷新数据
//            [self  insertRowAtTop];
        }
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.hometoxh=@"";
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"xianhuoVC"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.navigationController.navigationBar.hidden=YES;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];

    
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"商城货品";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(150);
        make.height.offset(30);
    }];
    
    
    //购物车按钮
    UIButton*rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"gouwuche"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(gouwucar) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.width.offset(22);
        make.height.offset(20);
        make.bottom.offset(-15);
    }];

    //定义数组
    arrayList=[[NSMutableArray alloc]init];
    
    //定义删选条件的数据
    self.sort=2;//默认为0
    
    if (![TongYong isBlankObjectAndStr:self.homeViewDic]) {
        
        
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"factoryCode"]]) {
            //生产商编码
            self.FactoryMappingID=self.homeViewDic[@"factoryCode"];
            
        }else{
            
            self.FactoryMappingID=@"";//生产商
            
        }
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"materialCode"]]) {
            //材质编码
            self.MaterialMappingID=self.homeViewDic[@"materialCode"];
            
        }else{
            
            self.MaterialMappingID=@"";//材质
            
        }
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"supplierId"]]) {
            //供应商编码
            self.Supplier_ID=self.homeViewDic[@"supplierId"];
            
        }else{
            
            self.Supplier_ID=@"";
            
        }
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"onlyDiscount"]]) {
            //是否特惠
            NSInteger isonly=[self.homeViewDic[@"onlyDiscount"] integerValue];
            if (isonly==0) {
                //全部
                self.IsOnlyDiscount=@"";
                
            }else if(isonly==1){
                
                self.IsOnlyDiscount=@"true";
                
            }
        }else
        {
            self.IsOnlyDiscount=@"";//是否特惠
            
        }
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"category"]]) {
            //产品类型
            self.CategoryName=self.homeViewDic[@"category"];
            
        }else{
        
            self.CategoryName=@"卷";
            
        }
        if (![TongYong isBlankObjectAndStr:self.homeViewDic[@"supplierId"]]) {
            //产品类型
            self.Supplier_ID=self.homeViewDic[@"supplierId"];
        }
        
        self.WeightRangeID=@"";//重量
        self.ThickCode=@"";//厚度
        self.SurfaceMappingID=@"";//表面
        self.WidthCode=@"";//宽度
        
    }else{
        
        //定义删选条件的数据
        
        self.CategoryName=@"卷";
        
        self.MaterialMappingID=@"";//材质
        self.SurfaceMappingID=@"";//表面
        self.FactoryMappingID=@"";//生产商
        self.WeightRangeID=@"";//重量
        self.ThickCode=@"";//厚度
        self.IsOnlyDiscount=@"";//是否特惠
        self.WidthCode=@"";//宽度
        self.Supplier_ID=@"";//供应商
    }
    
    
    
    if ([self.CategoryName isEqualToString:@"卷"]) {
        namestr=@"卷材";
    }else if([self.CategoryName isEqualToString:@"板"]){
        
        namestr=@"板材";
    }else if([self.CategoryName isEqualToString:@"管"]){
        
        namestr=@"管材";
    }else if([self.CategoryName isEqualToString:@"型"]){
        
        namestr=@"型材";
    }

    //定义删选菜单栏
    docview=[[UIView alloc]init];
    [self.view addSubview:docview];
    [docview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kNavigationBarHeight);
        make.left.offset(0);
        make.height.offset(38);
        make.right.offset(0);
    }];
    
    CZstatus=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, 38)];
    [CZstatus setTitleEdgeInsets:UIEdgeInsetsMake(0,0, 0, 20)];
    [CZstatus setImageEdgeInsets:UIEdgeInsetsMake(0, 70, 0, 0)];
    [CZstatus setTitle:namestr forState:UIControlStateNormal];
    CZstatus.titleLabel.font=Font(15);
    [CZstatus setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [CZstatus addTarget:self action:@selector(CZplay) forControlEvents:UIControlEventTouchUpInside];
    CZstatus.selected=NO;
    [docview addSubview:CZstatus];

    
    JGstatus=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3, 0, self.view.frame.size.width/3, 38)];
    [JGstatus setTitleEdgeInsets:UIEdgeInsetsMake(0,0, 0, 20)];
    [JGstatus setImageEdgeInsets:UIEdgeInsetsMake(0, 70, 0, 0)];
    [JGstatus setTitle:@"价格" forState:UIControlStateNormal];
    JGstatus.titleLabel.font=Font(15);
    [JGstatus setTitleColor:RGBCOLOR(102, 102, 102) forState:UIControlStateNormal];
    [JGstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [JGstatus addTarget:self action:@selector(JGplay) forControlEvents:UIControlEventTouchUpInside];
     JGstatus.selected=NO;
    [docview addSubview:JGstatus];
    
    
    SXbut=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width/3)*2, 0, self.view.frame.size.width/3, 38)];
    [SXbut setTitleEdgeInsets:UIEdgeInsetsMake(0,0, 0, 20)];
    [SXbut setImageEdgeInsets:UIEdgeInsetsMake(0, 70, 0, 0)];
    [SXbut setTitle:@"筛选" forState:UIControlStateNormal];
    SXbut.titleLabel.font=Font(15);
    [SXbut setTitleColor:RGBCOLOR(102, 102, 102) forState:UIControlStateNormal];
    [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [SXbut addTarget:self action:@selector(SXplay) forControlEvents:UIControlEventTouchUpInside];
     SXbut.selected=NO;
    [docview addSubview:SXbut];
    //分割线
    UIView *fenge=[[UIView alloc]initWithFrame:CGRectMake(0, 36, self.view.frame.size.width, 2)];
    fenge.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    [docview addSubview:fenge];

    
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,103, self.view.frame.size.width, self.view.frame.size.height-kNavigationBarHeight-kTabBarHeight)];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];
    
    //放置下拉上拉刷新控件
    __weak xianhuoVC *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf loadXianHuoData];
    }];
    
    // setup infinite scrolling
    [self.tableview addInfiniteScrollingWithActionHandler:^{
        [weakSelf insertRowAtBottom];
    }];

    //刷新数据
    [self insertRowAtTop];
    
    
    
    //判断是不是第一次启动应用
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        //如果是第一次启动的话,使用UserGuideViewController (用户引导页面) 作为根视图
        //做引导
        bantouming=[[UIView alloc]initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height-65)];
        bantouming.backgroundColor = [UIColor colorWithRed:(40/255.0f) green:(40/255.0f) blue:(40/255.0f) alpha:1.0f];
        bantouming.alpha = 0.4;
        [self.view addSubview:bantouming];
        [self.view bringSubviewToFront:bantouming];
        
        
        //引导图
        UIImageView *imagevc=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, self.view.frame.size.width-20, 102)];
        imagevc.image=[UIImage imageNamed:@"ic_xyh_mask_category"];
        [bantouming addSubview:imagevc];
        
        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
        
        [bantouming addGestureRecognizer:tapGesture];
    }

    //定义筛选的表格
    [self upCZtibView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertRowAtTop) name:kChangedLocationNotification object:nil];
}
//去购物车
-(void)gouwucar
{
    [self panduanCreate];
}

//判断是否创建公司
-(void)panduanCreate
{
    [Controller requestWithURL:POSTCHECK jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            YongHuQuanXuanModel *yongHuQuanXuanModel=[YongHuQuanXuanModel mj_objectWithKeyValues:data.data];
            NSString *IDnumString=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.COMPANY_ID];
            if ([IDnumString isEqualToString:@"0"]) {
                NSString *statusSt=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.STATUS];
                if ([TongYong isBlankObjectAndStr:yongHuQuanXuanModel.STATUS]||[statusSt isEqualToString:@"-1"])  {
                    //没有值便是 申请
                    //没有绑定公司 （提示是否加入公司  或者  创建公司  或者 输入验证码 加入公司）
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:@"您还没有“创建/加入”一家公司，不能进行该操作"
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:@"创建公司",@"查找公司",@"输入邀请码",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 43;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                }else{
                    //0申请中  -1被拒绝
                    
                    NSString *messageSt=[NSString stringWithFormat:@"您于%@申请加入“%@”，公司主管理员尚未同意您加入公司。",yongHuQuanXuanModel.APPLY_TIME,yongHuQuanXuanModel.APPLY_COMPANY_NAME];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:messageSt
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消申请"
                                                          otherButtonTitles:@"关闭",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 100;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                    
                }
                
            }else{
                
                gouwuCarVC *gwc = [[gouwuCarVC alloc] init];
                [self.navigationController pushViewController:gwc animated:YES];
                
            }
        }
    }];
    
    
}
//根据被点击按钮的索引处理点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==0) {
        if(buttonIndex==0){
            //取消
        }else if (buttonIndex==1) {
            //创建公司
            CreateCompanyVC*creatVC=[CreateCompanyVC new];
            [self.navigationController pushViewController:creatVC animated:YES];
            
        }else if(buttonIndex==2){
            //查找公司
            FindCompanyViewController*findCompanyViewController=[FindCompanyViewController new];
            [self.navigationController pushViewController:findCompanyViewController animated:NO];
            
        }else if (buttonIndex==3){
            //输入邀请码
            CompanyInviteCodeViewController *companyInviteCodeViewController=[CompanyInviteCodeViewController new];
            [self.navigationController pushViewController:companyInviteCodeViewController animated:YES];
        }
        
    }else if(alertView.tag==100){
        if (buttonIndex==0) {
            //取消申请
            [self quxiaoshenqingBut];
        }else{
        
        }
        
    }
}
//取消加入公司申请
-(void)quxiaoshenqingBut{
    
    [Controller requestWithURL:PostCancelApply jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            ALERTSHOW(@"你已成功取消加入该公司的申请")
        }else{
            
            ALERTSHOW(data.message);
        }
    }];
    
}

- (void)willPresentAlertView:(UIAlertView *)alertView{
    
    for( UIView * view in alertView.subviews ){
        
        if( [view isKindOfClass:[UILabel class]] )
        {
            UILabel* label = (UILabel*) view;
            
            label.textAlignment = NSTextAlignmentLeft;
        }
        
    }
}

- (void)loadShaiXuanResultData {
    NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithObject:self.CategoryName forKey:@"CategoryName"];
    [Controller requestWithURL:FILTEROPTIONS jsondic:tempDictionary finishBlock:^(ClassData* data) {
        if (data.number.intValue == 0) {
            if ([self.CategoryName isEqualToString:@"卷"] || [self.CategoryName isEqualToString:@"板"]) {
                self.customShaiXuanView.isShowPinLei = YES;
            } else {
                self.customShaiXuanView.isShowPinLei = NO;
            }
            self.customShaiXuanView.shaiXuanTiaoJianResultModel = [ShaiXuanTiaoJianResultModel mj_objectWithKeyValues:data.data];
        }
    }];
}

//蒙版
-(void)Actiondo:(id)sender{

    [bantouming removeFromSuperview];
    
}
//材质按钮方法
-(void)CZplay
{
    if (SXbut.selected==YES) {
        SXbut.selected=NO;
        [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
        
//        if (sx) {
//            sx.hidden=YES;
//            _tableview.hidden=NO;
//            [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
//        }
    }
    
    if (CZstatus.selected==NO) {
        
        [MobClick event:@"productCategories"];
        
        CZstatus.selected=YES;
        cztableview.hidden=NO;
        [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down-1"] forState:UIControlStateNormal];
//        sx.hidden=YES;
        
    }else if(CZstatus.selected==YES){
        CZstatus.selected=NO;
        cztableview.hidden=YES;
        [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    }


}

//价格按钮方法
-(void)JGplay
{
    if (SXbut.selected==YES) {
        SXbut.selected=NO;
        [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
        
//        if (sx) {
//            sx.hidden=YES;
//            _tableview.hidden=NO;
//            [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
//        }
    }

    if(CZstatus.selected==YES){
        CZstatus.selected=NO;
        cztableview.hidden=YES;
        [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    }

    
    
    if (JGstatus.selected==NO) {
        
        [MobClick event:@"priceSort"];
        
        JGstatus.selected=YES;
        [JGstatus setImage:[UIImage imageNamed:@"ic_arrow_down-1"] forState:UIControlStateNormal];
        
        self.sort=1;
        [self loadXianHuoData];
        
    }else if(JGstatus.selected==YES){
        JGstatus.selected=NO;
        [JGstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
        
        self.sort=2;
        [self loadXianHuoData];
    }

    
    
}

//筛选按钮方法
- (void)SXplay {
    if(CZstatus.selected==YES){
        CZstatus.selected=NO;
        cztableview.hidden=YES;
        [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    }
   
    if (SXbut.selected==NO) {
        [MobClick event:@"filter"];
        
        SXbut.selected = YES;
        [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down-1"] forState:UIControlStateNormal];
        //更新筛选条件
        if (_customShaiXuanView == nil) {
            [self loadShaiXuanResultData];
            self.customShaiXuanView.huaNanContainCityArray = self.huaNanContainCityArray;
            self.customShaiXuanView.huaNanContainCityString = self.CityNameList;
        } else {
            self.customShaiXuanView.huaNanContainCityString = self.selectedCityString;
            self.customShaiXuanView.huaNanContainCityArray = self.selectedCityArray;
        }
        [self.view addSubview:self.customShaiXuanView];
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    } else if (SXbut.selected == YES) {
        SXbut.selected = NO;
        [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
        [self closeIt];
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

//材质的表格
-(void)upCZtibView
{
    NSInteger indrom;
    
    //给材质的数组赋值
    CZarray=[[NSArray alloc] initWithObjects:@"卷材",@"板材",@"管材",@"型材",@"带材",nil];

    if ([self.CategoryName isEqualToString:@"卷"]) {
        indrom=0;
    }else if([self.CategoryName isEqualToString:@"板"]){
        
        indrom=1;
    }else if([self.CategoryName isEqualToString:@"管"]){
        
        indrom=2;
    }else if([self.CategoryName isEqualToString:@"型"]){
        
        indrom=3;
    }else if([self.CategoryName isEqualToString:@"带"]){
        
        indrom=4;
    }

    cztableview=[[CZtableview alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 250)];
    cztableview.delegate=self;
    [cztableview createCZtable:CZarray:indrom];
    [self.view addSubview:cztableview];
    [self.view bringSubviewToFront:cztableview];
    cztableview.hidden=YES;
    

}

-(void)didSelectCZ:(NSString *)czstring
{

    CZstatus.selected=NO;
    cztableview.hidden=YES;
    [CZstatus setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    
   [CZstatus setTitle:czstring forState:UIControlStateNormal];
    if ([czstring isEqualToString:@"卷材"]) {
        self.CategoryName=@"卷";
    } else if([czstring isEqualToString:@"板材"]){
        self.CategoryName=@"板";
    } else if([czstring isEqualToString:@"管材"]){
        self.CategoryName=@"管";
    } else if([czstring isEqualToString:@"型材"]){
        self.CategoryName=@"型";
    } else if([czstring isEqualToString:@"带材"]){
        self.CategoryName=@"带";
    }

    //重置筛选条件
    self.MaterialMappingID=@"";//材质
    self.SurfaceMappingID=@"";//表面
    self.FactoryMappingID=@"";//生产商
    self.WeightRangeID=@"";//重量
    self.ThickCode=@"";//厚度
    self.IsOnlyDiscount=@"";//是否特惠
    self.WidthCode=@"";//宽度
    self.Varieties = @"";// 品类
    
    [self loadXianHuoData];
    self.customShaiXuanView = nil;
}

- (void)insertRowAtTop {
    // 根据地区省份获取城市 用于判断公司是否被锁定
    NSString *provinceId = [[NSUserDefaults standardUserDefaults] objectForKey:kLocationRegion];
    NSDictionary *paramDictionary = [NSDictionary dictionaryWithObjectsAndKeys:provinceId,@"REGION", self.CategoryName, @"CategoryName", nil];
    [Controller requestWithURL:ContainCity jsondic:paramDictionary finishBlock:^(ClassData* data) {
        if (data.number.intValue == 0) {
            NSArray *citiesArray = [NSArray arrayWithArray:data.data];
            self.huaNanContainCityArray = citiesArray;
            NSMutableString *mutableString = [NSMutableString string];
            for (int i = 0; i < citiesArray.count; i++) {
                NSString *tempRegionButonString = citiesArray[i];
                if (0 == i) {
                    [mutableString appendString:tempRegionButonString];
                } else {
                    [mutableString appendFormat:@",%@", tempRegionButonString];
                }
            }
            self.CityNameList = mutableString;
            [self loadXianHuoData];
        }
    }];
}


- (void)insertRowAtBottom {
    
    __weak xianhuoVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    _page=_page+1;
    NSNumber*number1=[NSNumber numberWithInteger:_page];
    NSNumber*number2=[NSNumber numberWithInteger:10];
    NSNumber*number3=[NSNumber numberWithInteger:self.sort];
    
    NSMutableDictionary *di = [[NSMutableDictionary alloc] init];
    [di setValue:number1 forKey:@"page_num"];
    [di setValue:number2 forKey:@"page_step"];
    [di setValue:number3 forKey:@"Sort"];
    [di setValue:self.CategoryName forKey:@"CategoryName"];
    
    //    //重置筛选条件
    //    self.MaterialMappingID=@"";//材质
    //    self.SurfaceMappingID=@"";//表面
    //    self.FactoryMappingID=@"";//生产商
    //    self.WeightRangeID=@"";//重量
    //    self.ThickCode=@"";//厚度
    //    self.IsOnlyDiscount=@"";//是否特惠
    
    if (![TongYong isBlankObjectAndStr:self.MaterialMappingID]) {
        
        [di setValue:self.MaterialMappingID forKey:@"MaterialMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.SurfaceMappingID]) {
        
        [di setValue:self.SurfaceMappingID forKey:@"SurfaceMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.FactoryMappingID]) {
        
        [di setValue:self.FactoryMappingID forKey:@"FactoryMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.WeightRangeID]) {
        
        [di setValue:self.WeightRangeID forKey:@"WeightRangeID"];
    }
    if (![TongYong isBlankObjectAndStr:self.ThickCode]) {
        
        [di setValue:self.ThickCode forKey:@"ThickCode"];
    }
    if (![TongYong isBlankObjectAndStr:self.IsOnlyDiscount]) {
        
        [di setValue:self.IsOnlyDiscount forKey:@"IsOnlyDiscount"];
    }
    if (![TongYong isBlankObjectAndStr:self.WidthCode]) {
        
        [di setValue:self.WidthCode forKey:@"WidthCode"];
    }
    if (![TongYong isBlankObjectAndStr:self.Supplier_ID]) {
        
        [di setValue:self.Supplier_ID forKey:@"Supplier_ID"];
    }
    if (![TongYong isBlankObjectAndStr:self.CityNameList]) {
        
        [di setValue:self.CityNameList forKey:@"CityNameList"];
    }
    if (![TongYong isBlankObjectAndStr:self.Varieties]) {
        [di setValue:self.Varieties forKey:@"VarietiesNameList"];
    }
    
    NSString *provinceId = [[NSUserDefaults standardUserDefaults] objectForKey:kLocationRegion];
    if (provinceId) {
        [di setObject:provinceId forKey:@"ProvinceId"];
    }
    
    [Controller requestWithURL:BINDPRODUCT jsondic:di finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            xianhuoModel *xianhuoMd=[[xianhuoModel alloc]initWith:data.data];
            
            
            [arrayList addObjectsFromArray:xianhuoMd.ProductList];
            
            [self.tableview reloadData];
            
            
        }
        else
        {
            
        }
    }];
    
    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.infiniteScrollingView stopAnimating];
}

- (void)loadXianHuoData {
    __weak xianhuoVC *weakSelf = self;
    [weakSelf.tableview beginUpdates];
    
    _page=0;
    NSNumber*number1=[NSNumber numberWithInteger:0];
    NSNumber*number2=[NSNumber numberWithInteger:10];
    NSNumber*number3=[NSNumber numberWithInteger:self.sort];
    
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    
    [di setValue:number1 forKey:@"page_num"];
    [di setValue:number2 forKey:@"page_step"];
    [di setValue:number3 forKey:@"Sort"];
    [di setValue:self.CategoryName forKey:@"CategoryName"];
    
    if (![TongYong isBlankObjectAndStr:self.MaterialMappingID]) {
        [di setValue:self.MaterialMappingID forKey:@"MaterialMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.SurfaceMappingID]) {
        [di setValue:self.SurfaceMappingID forKey:@"SurfaceMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.FactoryMappingID]) {
        [di setValue:self.FactoryMappingID forKey:@"FactoryMappingID"];
    }
    if (![TongYong isBlankObjectAndStr:self.WeightRangeID]) {
        [di setValue:self.WeightRangeID forKey:@"WeightRangeID"];
    }
    if (![TongYong isBlankObjectAndStr:self.ThickCode]) {
        [di setValue:self.ThickCode forKey:@"ThickCode"];
    }
    if (![TongYong isBlankObjectAndStr:self.IsOnlyDiscount]) {
        [di setValue:self.IsOnlyDiscount forKey:@"IsOnlyDiscount"];
    }
    
    if (![TongYong isBlankObjectAndStr:self.WidthCode]) {
        [di setValue:self.WidthCode forKey:@"WidthCode"];
    }
    
    if (![TongYong isBlankObjectAndStr:self.Supplier_ID]) {
        [di setValue:self.Supplier_ID forKey:@"Supplier_ID"];
    }
    
    if (![TongYong isBlankObjectAndStr:self.CityNameList]) {
        [di setValue:self.CityNameList forKey:@"CityNameList"];
    }
    
    if (![TongYong isBlankObjectAndStr:self.Varieties]) {
        [di setValue:self.Varieties forKey:@"VarietiesNameList"];
    }
    
    NSString *provinceId = [[NSUserDefaults standardUserDefaults] objectForKey:kLocationRegion];
    if (provinceId) {
        [di setObject:provinceId forKey:@"ProvinceId"];
    }
    
    [Controller requestWithURL:BINDPRODUCT jsondic:di finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]) {
            xianhuoModel *xianhuoMd = [[xianhuoModel alloc]initWith:data.data];
            
            [arrayList removeAllObjects];
            [self.tableview reloadData];
            [arrayList addObjectsFromArray:xianhuoMd.ProductList];
            
            //判断 是否  有 无数据
            if (arrayList.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                la=[[labelview alloc]init];
                [la setimage:@"wusousuoNew" tit:@"暂无货品资源\n快试试“吆喝”采购功能吧" buttontit:@"去采购" frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-110)/2, 50, 110, 112);
                la.label.frame=CGRectMake(50, 172, self.view.frame.size.width-100, 40);
                la.button.frame=CGRectMake(40, 222, self.view.frame.size.width-80, 40);
                la.delegate=self;
                [_tableview addSubview:la];
            }else{
                if (la) {
                    [la removeFromSuperview];
                }
                [self.tableview reloadData];
            }
            //重新把供应商社为空
            self.Supplier_ID=@"";
        }
    }];
    
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.pullToRefreshView stopAnimating];
}

-(void)didClickButtonInBottomView {
    RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
    [rootTabBarViewController changeRootViewControllerTo:2];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrayList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 98;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"XianhuoListCell";
    
    listdata=[[xianhuoList alloc ]initWith:arrayList[indexPath.row]];
    
    
    
    //自定义cell类有图片
    xianhuoNoimageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        //通过xib的名称加载自定义的cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"xianhuoNoimageTableViewCell" owner:self options:nil] lastObject];
    }
    
    //拼接顶部lable信息
    NSString *toplable;
//    if ([TongYong isBlankObjectAndStr:listdata.SurfaceCode]) {
//         toplable=[NSString stringWithFormat:@"%@ %@",listdata.TopCategoryName,listdata.MaterialCode];
//    }else{
//         toplable=[NSString stringWithFormat:@"%@ %@/%@",listdata.TopCategoryName,listdata.MaterialCode,listdata.SurfaceCode];
//
//    }
   
    if ([TongYong isBlankObjectAndStr:listdata.SurfaceCode]) {
        toplable=[NSString stringWithFormat:@"%@ %@",listdata.CategoryName,listdata.MaterialCode];
    }else{
        toplable=[NSString stringWithFormat:@"%@ %@/%@",listdata.CategoryName,listdata.MaterialCode,listdata.SurfaceCode];
        
    }
    cell.topLable.text=toplable;
   
    //拼接价格
    NSString *priceUnit;
    
    NSInteger prt=[listdata.PriceUnit integerValue];
    if (prt==10) {
        
        priceUnit=@"元/吨";
    }else if (prt==20) {
        
        priceUnit=@"元/支";
    }else if (prt==30) {
        
        priceUnit=@"元/千克";
    }else if (prt==40) {
        
        priceUnit=@"元/张";
    }
    
    
    NSString *tep_peice=[NSString stringWithFormat:@"%@",listdata.Price];
    //价格
    NSDecimalNumber *priceNumber = [[NSDecimalNumber alloc] initWithString:tep_peice];//价格
    
    NSString *price=[NSString stringWithFormat:@"￥%@%@",[NSString notRounding:priceNumber afterPoint:2],priceUnit];
    cell.danjia.text=price;
    
    //拼接大名称的字段  规格(参厚)/等级/边部/包装
    NSString *xinhuoLableName;
    
    BOOL package=[TongYong isBlankObjectAndStr:listdata.PackageName];//没有包装
    
    BOOL EdgeName=[TongYong isBlankObjectAndStr:listdata.EdgeName];//没有边部
    
    BOOL LevelName=[TongYong isBlankObjectAndStr:listdata.LevelName];//没有等级
    
    
    if (!package&&!EdgeName&&!LevelName) {
        
        //三个都有
    xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@/%@/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.LevelName,listdata.EdgeName,listdata.PackageName];

    }else if (package)
     {
         if (EdgeName&&!LevelName) {
             
         xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.LevelName];
             
         }else if (!EdgeName&&!LevelName){
             
         xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.LevelName,listdata.EdgeName];
         
         }else if (!EdgeName&&LevelName){
         xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.EdgeName];
         
         }else if (EdgeName&&LevelName){
             
          xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)",listdata.SpecCode,listdata.ReferenceThick.floatValue];
         
         }
     }else if (EdgeName)
     {
         if (package&&!LevelName) {
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.LevelName];
             
         }else if (!package&&!LevelName){
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.LevelName,listdata.PackageName];
             
         }else if (!package&&LevelName){
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.PackageName];
             
         }else if (package&&LevelName){
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)",listdata.SpecCode,listdata.ReferenceThick.floatValue];
         }
     }else if (LevelName)
     {
     
         if (package&&!EdgeName) {
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.EdgeName];
             
         }else if (!package&&!EdgeName){
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.EdgeName,listdata.PackageName];
             
         }else if (!package&&EdgeName){
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)/%@",listdata.SpecCode,listdata.ReferenceThick.floatValue,listdata.PackageName];
             
         }else if (package&&EdgeName){
             
             xinhuoLableName=[NSString stringWithFormat:@"%@(%.2f)",listdata.SpecCode,listdata.ReferenceThick.floatValue];
             
         }
     }
    

    cell.titleLableName.text=xinhuoLableName;
    
   //拼接地址
    NSString *addst;
    NSString *housenamest;
    if ([TongYong isBlankObjectAndStr:listdata.HouseName]) {
        housenamest = @"";
    }else{
        housenamest = listdata.HouseName;
    }
    
    if ([TongYong isBlankObjectAndStr:listdata.CityName]) {
        //如果是空得
        addst=[NSString stringWithFormat:@"%@",housenamest];
    }else{
        addst=[NSString stringWithFormat:@"%@  %@",housenamest,listdata.CityName];
    }
    
    cell.add.text=addst;

    //特惠图标
    NSInteger ispre=[listdata.IsPreferential integerValue];
    
    if (ispre==1) {
        cell.TehuiImage.hidden=NO;
        cell.TehuiImage.image=[UIImage imageNamed:@"ic_preferential"];
    }else{
        cell.TehuiImage.hidden = YES;
    }

    
    //货品销售模式 0 一口价  30定制
    NSInteger saleMode=[listdata.SaleMode  integerValue];
    
    if (saleMode == 0) {
        //现货
        cell.saleImage.image=[UIImage imageNamed:@"biaoshi_xianhuo"];
        
    }else if(saleMode == 30){
    
        cell.saleImage.image=[UIImage imageNamed:@"biaoshi_dingzhi"];

    }else{
    
        cell.saleImage.hidden=YES;
        
    }
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [MobClick event:@"viewItemDetails"];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 进入详情页面
    xianhuoDatailView *detailsVC = [[xianhuoDatailView alloc] init];
    xianhuoList *listdata1 = [[xianhuoList alloc] initWith:arrayList[indexPath.row]];
    detailsVC.Datalist = listdata1;
    [self.navigationController pushViewController:detailsVC animated:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - lazy load
- (CustomShaiXuanView *)customShaiXuanView {
    if (!_customShaiXuanView) {
        _customShaiXuanView = [[CustomShaiXuanView alloc] initWithFrame:CGRectMake(0, 100, self.view.width, self.view.height-50)];
    }
    __block xianhuoVC *weakSelf = self;
    _customShaiXuanView.customShaiXuanViewCancelOrConformBlock = ^(NSMutableDictionary *blockDictionary) {
        [weakSelf closeIt];
        if (blockDictionary == nil) {
            weakSelf.customShaiXuanView = nil;
            weakSelf.selectedCityString = nil;
            weakSelf.selectedCityArray = nil;
        } else {
            // 点击确认按钮操作
            weakSelf.MaterialMappingID = blockDictionary[@"MaterialMappingID"];
            weakSelf.SurfaceMappingID = blockDictionary[@"SurfaceMappingID"];
            weakSelf.FactoryMappingID = blockDictionary[@"FactoryMappingID"];
            weakSelf.WeightRangeID = blockDictionary[@"WeightRangeID"];
            weakSelf.ThickCode = blockDictionary[@"ThickCode"];
            weakSelf.IsOnlyDiscount = blockDictionary[@"IsOnlyDiscount"];
            weakSelf.WidthCode = blockDictionary[@"WidthCode"];
            NSMutableString *cityNameList = blockDictionary[@"CityNameList"];
            weakSelf.selectedCityString = cityNameList;
            weakSelf.selectedCityArray = [cityNameList componentsSeparatedByString:@","];
            weakSelf.Varieties = blockDictionary[@"VarietiesNameList"];
            
            [weakSelf loadXianHuoData];
        }
    };
    return _customShaiXuanView;
}

- (void)closeIt {
    SXbut.selected = NO;
    [SXbut setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    [self.customShaiXuanView removeFromSuperview];
}

@end
